package com.ymatou.iapi.trading.operate.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.req.ChangeOrderNoteBean;
import com.ymatou.iapi.trading.operate.service.ChangeOrderNoteCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 修改订单备注 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ChangeOrderNote {

    private static ChangeOrderNoteBean changeordernotebean;
    private static ChangeOrderNoteCall changeordernotecall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("修改订单备注");
    }

    @Before
    public void caseUp() {

        changeordernotebean = new ChangeOrderNoteBean();
        changeordernotecall = new ChangeOrderNoteCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(changeordernotecall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ChangeOrderNote_001() {
        Logger.start(true, "修改订单备注成功");
        try {
            Logger.comment("前置数据准备");
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();
            // 保留两位小数
            Logger.comment("前置数据准备結束");
            String remarkContent = "修改订单备注";
            int i = 0;
            while (i < 2) {
                changeordernotebean.setOrderId(orderId);
                remarkContent = remarkContent + i;
                changeordernotebean.setRemarkContent(remarkContent);
                changeordernotebean.setUserId(userId);
                changeordernotebean.setRemarkLevel(1);
                // 发送时间
                String date = YMTDateUtil.getSysTime();
                changeordernotebean.setRemarkTime(date);
                // 发送请求
                changeordernotecall.setData(changeordernotebean);
                changeordernotecall.callService();
                Logger.verifyEquals(ECode.SUCCESS, changeordernotecall.getCode(), "验证BCode");
                i++;
            }
            // 验证数据库
            DefaultWapper database = new DefaultWapper();
            List<Map> orderNoteL = database.selectOrderNoteByOrderId(orderId);
            Logger.verifyEquals(1, orderNoteL.size(), "验证Ymt_O_OrderNote表增加一条记录");
            Logger.verifyEquals(remarkContent, orderNoteL.get(0).get("sContent"),
                    "验证备注内容");
            Logger.verifyEquals(changeordernotebean.getRemarkLevel(), (Integer) orderNoteL.get(0).get("iRemarkLevel"),
                    "验证iRemarkLevel");
            Logger.verifyEquals(1, database.selectOrderNoteHistoryByOrderId(orderId).size(),
                    "验证Ymt_O_OrderNoteHistory表增加一条记录");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P3.class)
    @TestCase
    public void Tc_ChangeOrderNote_002() {
        Logger.start(true, "订单不存在，修改订单失败");
        try {
            int userId = 1113;
            Integer orderId = 205254725;
            String remarkContent = "修改订单备注";
            changeordernotebean.setOrderId(orderId);
            changeordernotebean.setRemarkContent(remarkContent);
            changeordernotebean.setUserId(userId);
            // 发送时间
            String date = YMTDateUtil.getSysTime();
            changeordernotebean.setRemarkTime(date);
            // 发送请求
            changeordernotecall.setData(changeordernotebean);
            changeordernotecall.callService();
            Logger.verifyEquals(ECode.FAILED201, changeordernotecall.getCode(), "验证BCode");
            Logger.verifyIsContains(ECode.FAILED201_MSG_11, changeordernotecall.getMsg(), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P3.class)
    @TestCase
    public void Tc_ChangeOrderNote_003() {
        Logger.start(false, "用户不存在，修改订单失败");
        try {
            Logger.comment("前置数据准备");
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int tradingId = placeOrderResp.getTradingId();
            // 保留两位小数
            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            Logger.comment("前置数据准备结束");
            int userId = 1;
            String remarkContent = "修改订单备注";
            changeordernotebean.setOrderId(orderId);
            changeordernotebean.setRemarkContent(remarkContent);
            changeordernotebean.setUserId(userId);
            // 发送时间
            String date = YMTDateUtil.getSysTime();
            changeordernotebean.setRemarkTime(date);
            // 发送请求
            changeordernotecall.setData(changeordernotebean);
            changeordernotecall.callService();
            Logger.verifyEquals(ECode.FAILED201, changeordernotecall.getCode(), "验证BCode");
            Logger.verifyEquals("用户"+changeordernotebean.getUserId()+"不是订单"+changeordernotebean.getOrderId()+"的所有者", changeordernotecall.getMsg(), "验证BCode");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P3.class)
    @TestCase
    public void Tc_ChangeOrderNote_004() {
        Logger.start(false, "用户id为null，修改订单失败");
        try {
            Logger.comment("前置数据准备");
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int tradingId = placeOrderResp.getTradingId();
            // 保留两位小数
            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            Logger.comment("前置数据准备结束");
            int userId = 1;
            String remarkContent = "修改订单备注";
            changeordernotebean.setOrderId(orderId);
            changeordernotebean.setRemarkContent(remarkContent);
            // changeordernotebean.setUserId(userId);
            // 发送时间
            String date = YMTDateUtil.getSysTime();
            changeordernotebean.setRemarkTime(date);
            // 发送请求
            changeordernotecall.setData(changeordernotebean);
            changeordernotecall.callService();
            Logger.verifyEquals(ECode.ERROR400, changeordernotecall.getCode(), "验证BCode");
            Logger.verifyEquals("userId:用户ID不能为0", changeordernotecall.getMsg(), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P3.class)
    @TestCase
    public void Tc_ChangeOrderNote_005() {
        Logger.start(false, "订单id为null");
        try {
            int userId = 1;
            String remarkContent = "修改订单备注";
            // changeordernotebean.setOrderId(orderId);
            changeordernotebean.setRemarkContent(remarkContent);
            changeordernotebean.setUserId(userId);
            // 发送时间
            String date = YMTDateUtil.getSysTime();
            changeordernotebean.setRemarkTime(date);
            // 发送请求
            changeordernotecall.setData(changeordernotebean);
            changeordernotecall.callService();
            Logger.verifyEquals(ECode.ERROR400, changeordernotecall.getCode(), "验证BCode");
            Logger.verifyIsContains(ECode.ERROR400_MSG_9, changeordernotecall.getMsg(), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ChangeOrderNote_006() {
        Logger.start(true, "修改备注时,不传备注等级，iRemarkLevel为上次保存的备注等级 ");
        try {
            Logger.comment("前置数据准备");
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();
            // 保留两位小数
            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额            
            Logger.comment("前置数据准备结束");
   
            String remarkContent = "修改订单备注";
            changeordernotebean.setOrderId(orderId);
            remarkContent = remarkContent;
            changeordernotebean.setRemarkContent(remarkContent);
            changeordernotebean.setUserId(userId);
            changeordernotebean.setRemarkLevel(1);
            // 发送时间
            String date = YMTDateUtil.getSysTime();
            changeordernotebean.setRemarkTime(date);
            // 发送请求
            changeordernotecall.setData(changeordernotebean);
            changeordernotecall.callService();
            Logger.verifyEquals(ECode.SUCCESS, changeordernotecall.getCode(), "验证BCode");
            // 验证数据库
            DefaultWapper database = new DefaultWapper();
            List<Map> orderNoteL = database.selectOrderNoteByOrderId(orderId);
            Logger.verifyEquals(1, orderNoteL.size(), "验证Ymt_O_OrderNote表增加一条记录");
            Logger.verifyEquals(remarkContent, orderNoteL.get(0).get("sContent"),
                    "验证备注内容");
            Logger.verifyEquals(changeordernotebean.getRemarkLevel(), (Integer) orderNoteL.get(0).get("iRemarkLevel"),
                    "验证iRemarkLevel");
            // 第二次不传remarklevel
            changeordernotebean.setRemarkLevel(null);
            changeordernotecall.setData(changeordernotebean);
            changeordernotecall.callService();
            Logger.verifyEquals(ECode.SUCCESS, changeordernotecall.getCode(), "验证BCode");
            orderNoteL = database.selectOrderNoteByOrderId(orderId);
            Logger.verifyEquals(1, orderNoteL.size(), "验证Ymt_O_OrderNote表增加一条记录");
            Logger.verifyEquals(remarkContent, orderNoteL.get(0).get("sContent"),
                    "验证备注内容");
            Logger.verifyEquals(1, (Integer) orderNoteL.get(0).get("iRemarkLevel"),
                    "验证iRemarkLevel为上次保存的备注等级");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
}
